﻿using MediatR;
using Schedulingclasses.Api.User.Command;
using Schedulingclasses.Domain;
using Schedulingclasses.ErrorCode;
using Schedulingclasses.Infrastructure;

namespace Schedulingclasses.Api.User.Handler
{
    public class UpdateInstanceHandler : IRequestHandler<UpdateInstanceCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<instancce> instance;
        private readonly ILogger<UpdateInstanceHandler> logger;
        private readonly IBaseRepository<ProcessInfo> processinfo;

        public UpdateInstanceHandler(IBaseRepository<instancce> instance,ILogger<UpdateInstanceHandler> logger,IBaseRepository<ProcessInfo> processinfo)
        {
            this.instance = instance;
            this.logger = logger;
            this.processinfo = processinfo;
        }

        public async Task<ApiResult<int>> Handle(UpdateInstanceCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var res=new ApiResult<int>();

                var instances = await instance.GetValue(x => x.instancceId.Equals(request.instancceId));

                if (request.StatusId==1)
                {
                    instances.Nodenumber = instances.Lowernode;
                    instances.Nodename = instances.Lowernodename;
                    instances.UserId= instances.LoUserId;
                    instances.UserName= instances.LoUserName;

                    instances.Approvalopinion=request.Approvalopinion;

                    if (instances.Nodenumber=="")
                    {
                        instances.StatusId = 2;
                        instances.Status = "通过";
                    }
                    else
                    {
                        var processinfos = await processinfo.GetValue(x => x.Nodenumber.Equals(instances.Nodenumber));
                        var processinfose = await processinfo.GetValue(x=>x.Nodenumber.Equals(processinfos.Lowernode));
                        if (processinfos.Lowernode=="")
                        {
                            instances.Lowernode = "";
                            instances.Lowernodename = "";
                            instances.LoUserId = 0;
                            instances.LoUserName = "";
                        }
                        else
                        {
                            instances.Lowernode = processinfose.Nodenumber;
                            instances.Lowernodename = processinfose.Nodename;
                            instances.LoUserId = processinfose.UserId;
                            instances.LoUserName = processinfose.Username;
                        }
                    }
                }

                else
                {
                    instances.StatusId = 0;
                    instances.Status = "驳回";

                    await instance.Update(instances);

                    res.code=ApiREunm.Fail;
                    res.msg = "该申请已驳回";
                    return res;
                }

                var updateste = await instance.Update(instances);

                res.code = ApiREunm.OK;
                res.msg = "申请已通过";
                return res;

            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }
    }
}
